﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using RBAC.ORM;//请先添加引用
namespace RBAC.ORM.DAL
{
    /// <summary>
    /// 数据访问类SDepartment。
    /// </summary>
    public class SDepartment
    {
        public SDepartment()
        { }
        #region  成员方法

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from SDepartment");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.NVarChar,50)};
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(RBAC.ORM.Model.SDepartment model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into SDepartment(");
            strSql.Append("id,name,description,uppart,sequence,visiable,ifRoot,ifLeaf)");
            strSql.Append(" values (");
            strSql.Append("@id,@name,@description,@uppart,@sequence,@visiable,@ifRoot,@ifLeaf)");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.NVarChar,50),
					new SqlParameter("@name", SqlDbType.NVarChar,50),
					new SqlParameter("@description", SqlDbType.NVarChar,200),
					new SqlParameter("@uppart", SqlDbType.NVarChar,50),
					new SqlParameter("@sequence", SqlDbType.SmallInt,2),
					new SqlParameter("@visiable", SqlDbType.SmallInt,2),
					new SqlParameter("@ifRoot", SqlDbType.SmallInt,2),
					new SqlParameter("@ifLeaf", SqlDbType.SmallInt,2)};
            parameters[0].Value = model.id;
            parameters[1].Value = model.name;
            parameters[2].Value = model.description;
            parameters[3].Value = model.uppart;
            parameters[4].Value = model.sequence;
            parameters[5].Value = model.visiable;
            parameters[6].Value = model.ifRoot;
            parameters[7].Value = model.ifLeaf;
            try
            {
                DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(RBAC.ORM.Model.SDepartment model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update SDepartment set ");
            strSql.Append("name=@name,");
            strSql.Append("description=@description,");
            strSql.Append("uppart=@uppart,");
            strSql.Append("sequence=@sequence,");
            strSql.Append("visiable=@visiable,");
            strSql.Append("ifRoot=@ifRoot,");
            strSql.Append("ifLeaf=@ifLeaf");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.NVarChar,50),
					new SqlParameter("@name", SqlDbType.NVarChar,50),
					new SqlParameter("@description", SqlDbType.NVarChar,200),
					new SqlParameter("@uppart", SqlDbType.NVarChar,50),
					new SqlParameter("@sequence", SqlDbType.SmallInt,2),
					new SqlParameter("@visiable", SqlDbType.SmallInt,2),
					new SqlParameter("@ifRoot", SqlDbType.SmallInt,2),
					new SqlParameter("@ifLeaf", SqlDbType.SmallInt,2)};
            parameters[0].Value = model.id;
            parameters[1].Value = model.name;
            parameters[2].Value = model.description;
            parameters[3].Value = model.uppart;
            parameters[4].Value = model.sequence;
            parameters[5].Value = model.visiable;
            parameters[6].Value = model.ifRoot;
            parameters[7].Value = model.ifLeaf;

            try
            {
                DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from SDepartment ");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.NVarChar,50)};
            parameters[0].Value = id;

            try
            {
                DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public RBAC.ORM.Model.SDepartment GetModel(string id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,name,description,uppart,sequence,visiable,ifRoot,ifLeaf from SDepartment ");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.NVarChar,50)};
            parameters[0].Value = id;

            RBAC.ORM.Model.SDepartment model = new RBAC.ORM.Model.SDepartment();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                model.id = ds.Tables[0].Rows[0]["id"].ToString();
                model.name = ds.Tables[0].Rows[0]["name"].ToString();
                model.description = ds.Tables[0].Rows[0]["description"].ToString();
                model.uppart = ds.Tables[0].Rows[0]["uppart"].ToString();
                if (ds.Tables[0].Rows[0]["sequence"].ToString() != "")
                {
                    model.sequence = int.Parse(ds.Tables[0].Rows[0]["sequence"].ToString());
                }
                if (ds.Tables[0].Rows[0]["visiable"].ToString() != "")
                {
                    model.visiable = int.Parse(ds.Tables[0].Rows[0]["visiable"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ifRoot"].ToString() != "")
                {
                    model.ifRoot = int.Parse(ds.Tables[0].Rows[0]["ifRoot"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ifLeaf"].ToString() != "")
                {
                    model.ifLeaf = int.Parse(ds.Tables[0].Rows[0]["ifLeaf"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,name,description,uppart,sequence,visiable,ifRoot,ifLeaf ");
            strSql.Append(" FROM SDepartment ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,name,description,uppart,sequence,visiable,ifRoot,ifLeaf ");
            strSql.Append(" FROM SDepartment ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "SDepartment";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  成员方法
    }
}


